package com.code.kaiser;

/**
 * KaiserDemo
 *
 * <p>凯撒加密与解密
 */
public class KaiserDemo {
  public static void main(String[] args) {
    // 定义原文
    String input = "Hello World";
    // 把原文右边移动3位
    int key = 3;
    // 凯撒加密
    String s = encryptKaiser(input, key);
    System.out.println("加密==" + s);
    String s1 = decryptKaiser(s, key);
    System.out.println("明文==" + s1);
  }

  /**
   * 凯撒-解密
   *
   * @param s 密文
   * @param key 密钥
   * @return
   */
  public static String decryptKaiser(String s, int key) {
    char[] chars = s.toCharArray();
    StringBuilder sb = new StringBuilder();
    for (char aChar : chars) {
      int b = aChar;
      // 偏移数据
      b -= key;
      char newb = (char) b;
      sb.append(newb);
    }
    return sb.toString();
  }

  /**
   * 凯撒-加密
   *
   * @param input 原文
   * @param key 把原文右边移动key位
   * @return
   */
  public static String encryptKaiser(String input, int key) {
    // 抽取快捷键 ctrl + alt + m
    // 把字符串变成字节数组
    char[] chars = input.toCharArray();
    StringBuilder sb = new StringBuilder();
    for (char aChar : chars) {
      int b = aChar;
      // 往右边移动3位
      b = b + key;
      char newb = (char) b;
      sb.append(newb);
    }
    //        System.out.println("密文==="+sb.toString());
    return sb.toString();
  }
}
